package face.pay.sharing.biz;

import com.alibaba.fastjson.JSON;
import com.face.pay.WxApiConfig;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;
import face.pay.dao.SharingOrderInfoDao;
import face.pay.entity.CpnCardDetailInfo;
import face.pay.entity.EnterpriseInfo;
import face.pay.entity.OrderInfo;
import face.pay.entity.SharingOrderInfo;
import face.pay.util.MathUtil;
import face.pay.vo.VSharingAccount;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 分账
 */
@Service
@Slf4j
public class SharingQueryBiz {

    private final static   String domain    = "api.mch.weixin.qq.com";

    /**
     * secapi/pay/profitsharing
     */
    private final static   String urlSuffix = "/pay/profitsharingquery";



    @Autowired
    private SharingOrderInfoDao sharingOrderInfoDao;





    public SharingOrderInfo query(WxApiConfig wxApiConfig, OrderInfo orderInfo, EnterpriseInfo enterpriseInfo,String outOrderNo){

        log.info("查询账单");
        try{

                Long orderId = orderInfo.getId();

                String appId = wxApiConfig.getAppID();
                String apiKey = wxApiConfig.getKey();
                String mchId = wxApiConfig.getMchID();

                String tradeNo = orderInfo.getTradeNo();

                String subMchId = enterpriseInfo.getWxSubMchId();

                WXPayRequest wxPayRequest = new WXPayRequest(wxApiConfig);

                String nonce_str = WXPayUtil.generateNonceStr();

                Map<String, String> map = new HashMap<>();
                map.put("mch_id", mchId);
                map.put("nonce_str", nonce_str);
                map.put("sub_mch_id", subMchId);
                map.put("transaction_id", tradeNo);

                map.put("out_order_no", outOrderNo);

                String xml = WXPayUtil.generateSignedXml(map, apiKey, WXPayConstants.SignType.HMACSHA256);
                log.info("发送请求 " + xml);
                String result = wxPayRequest.requestOnce(domain, urlSuffix, xml, true);
                log.info("返回结果 " + result);
                Map<String, String> resultMap = WXPayUtil.xmlToMap(result);


        }catch (Exception e){
             log.error("查询账单",e);
        }
        return  null;

    }


}
